#include "launcher.h"

launcher::launcher()
{
    baseheight = 10;
    basewidth = 50;
    pivotcenterpoint.setX(basewidth/2);
    pivotcenterpoint.setY(baseheight);
    pivotradius = basewidth/3;
    shaftlength = basewidth;
    shaftheight = 5;
//    shafttheta = 3.14159265/4;
    shafttheta = 0.0174532925;
    mintheta = 0.0174532925;
}

void launcher::DrawLauncher(QPainter &painter)
{
    // drawn back to front
    DrawShaft(painter);
    DrawPivot(painter);
    DrawBase(painter);
}

void launcher::DrawBase(QPainter &painter)
{
    // temp variables
    double tempx = 0;
    double tempy = baseheight;
    double tempbh = 0;
    double tempbw = basewidth;

    QPoint temptl;
    QPoint tempbr;

    // init pen
    QPen pen;
    pen.setWidth(2);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::blue);

    // converts the points
    CoordConverter.WindowtoViewPort(tempx, tempy);
    CoordConverter.WindowtoViewPort(tempbw, tempbh);

    // initializes the Qpoints
    temptl.setX(tempx);
    temptl.setY(tempy);
    tempbr.setX(tempbw);
    tempbr.setY(tempbh);

    // declared here for ease of use
    QRect baserect(temptl, tempbr);
    // draws the rect
    painter.drawRect(baserect);
}

void launcher::DrawPivot(QPainter &painter)
{
    double tempx = pivotcenterpoint.rx();
    double tempy = pivotcenterpoint.ry();
    QPoint tempp;

    // init pen
    QPen pen;
    pen.setWidth(2);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::green);

    // convert the coordinate
    CoordConverter.WindowtoViewPort(tempx, tempy);

    // set to QPoint variable
    tempp.setX(tempx);
    tempp.setY(tempy);

    // draw the circle
    painter.drawEllipse(tempp, pivotradius, pivotradius);

}

void launcher::DrawShaft(QPainter &painter)
{
    // rotation based deltas
    double xhat = 0;
    double yhat = 0;

    // temp variables to not overwrite the normal ones
    double tempx = pivotcenterpoint.rx();
    double tempy = pivotcenterpoint.ry();
    double tempbh;
    double tempbw;

    // QPoints for easier use of the qrect functoin
    QPoint temptl;
    QPoint tempbr;

    // init pen
    QPen pen;
    pen.setWidth(2);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::cyan);

    // find the deltas
    xhat = sin(shafttheta) * (0.5 * shaftheight);
    yhat = cos(shafttheta) * (0.5 * shaftheight);

    // initialize the temp vars
    tempx += xhat;
    tempy += yhat;
    tempbw = tempx + shaftlength;
    tempbh = tempy + shaftheight;

    // convert the points
    CoordConverter.WindowtoViewPort(tempx, tempy);
    CoordConverter.WindowtoViewPort(tempbw, tempbh);

    // set the QPoints to the temps
    temptl.setX(tempx);
    temptl.setY(tempy);
    tempbr.setX(tempbw);
    tempbr.setY(tempbh);

    // declared QRect down here for ease of use
    QRect shaftrect(temptl, tempbr);

    // saves the painter for the transformations
    painter.save();

    // required transformations to rotate
    painter.translate(temptl);
    painter.rotate(-shafttheta * (180/PI));
    painter.translate(-temptl);

    // draws the rectangle
    painter.drawRect(shaftrect);

    // counter transformation
    painter.rotate(shafttheta * (180/PI));

    // restores the painter
    painter.restore();

    // initializes the right section of the shaft for later use // ugly use here, another way?
    botrightshaft = shaftrect.bottomRight();
    toprightshaft = shaftrect.topRight();

}

QPoint launcher::ReturnInitBallLocation()
{
    // temporary distance var
    double distance;
    // new x and y coords
    int newx;
    int newy;
    QPoint ballinitloc; // the point that is to be returned

    // find midpoint of topright and bottom right of shaft
    int midx = (toprightshaft.rx() + botrightshaft.rx())/2;
    int midy = (toprightshaft.ry() + botrightshaft.ry())/2;

    //calculate slope
    double slope = (toprightshaft.ry() - botrightshaft.ry())/((toprightshaft.rx() - botrightshaft.rx()) || 0.0000000000000000001);
    // inverse recipricol is the new slope
    slope = -1/slope;

    // solve for the new x and y values
    // the first equation has a rearranged version of point slope form inserted into the distance equation which is then rearranged
    // the second equation is the rearranged version of the point slope equation.  since newx was already solved for no need for another complicated equation
    newx = (sqrt(pow(distance, 2)/(pow(slope, 2) + 1))) + midx;
    newy = (slope * (newx - midx)) + midy;

    // puts the new coordinate into the point
    ballinitloc.setX(newx);
    ballinitloc.setY(newy);

    return ballinitloc;
}


